package com.ruoyi.project.cms.warehouse.controller;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.warehouse.domain.CmsStockGoods;
import com.ruoyi.project.cms.warehouse.domain.CmsStockInfo;
import com.ruoyi.project.cms.warehouse.service.IInfoService;
import com.ruoyi.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("cms/warehouse/info")
public class WarehouseInfoController extends BaseController {

    final private String prefix="cms/warehouse/info";

    @Autowired
    private IInfoService iInfoService;

    @GetMapping("/info")
    public String getInfo(){
        return prefix+"/info";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo getInfosList(CmsStockInfo info){
        startPage();
        List<CmsStockInfo> infos = iInfoService.selectAll(info);
        System.out.println(infos.toString());
        return  getDataTable(infos);
    }


    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax( iInfoService.deleteByid(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }


    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CmsStockInfo byId = iInfoService.findById(id);
        byId.setDesc(null);
        mmap.put("info",byId );
        return prefix + "/edit";
    }


    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated CmsStockInfo info)
    {
        User sysUser = getSysUser();
        @NotBlank(message = "登录账号不能为空") @Size(min = 0, max = 30, message = "登录账号长度不能超过30个字符") String loginName = sysUser.getLoginName();
        info.setCreateUser(loginName);
        String time = DateUtils.getTime();
        Date date = DateUtils.parseDate(time);
        info.setCreateTime(date);
        info.setFlag("修改");
        return toAjax(iInfoService.addInfo(info));
    }


}
